<template>
  <div id="home">
    <nav-bar class="nav-bar">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :title="['流行','新款','精选']"
                 class="tab-control"
                 ref="tabcontrol2"
                 v-show="tbShow"
                 @tabclick="tabclick"></tab-control>
    <scroll class="content"
            ref="scroll"
            :probeType="3"
            :pullUpLoad="true"
            @scroll="apear"
            @pullingUp="loadmore">
    <home-swiper :banner="banner" @swiperLoaded="spLoaded"></home-swiper>
    <recommend-view :recommends="recommends"></recommend-view>
    <feature-view></feature-view>
    <tab-control :title="['流行','新款','精选']"
                 class="stick"
                 ref="tabcontrol1"
                 @tabclick="tabclick"></tab-control>
    <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <back-top @click.native="backclick" v-show="ishow"/>
  </div>
</template>

<script>
  import NavBar from 'components/common/navbar/NavBar.vue'
  import HomeSwiper from './childhome/HomeSwiper'
  import RecommendView from './childhome/RecommendView'
  import FeatureView from './childhome/FeatureView'
  import TabControl from 'components/content/tabControl/TabControl'
  import GoodsList from 'components/content/goods/GoodsList'
  import Scroll from 'components/common/scroll/Scroll'
  import BackTop from 'components/content/backtop/BackTop'

  import {getHomeData,getHomeGoods} from "../../network/home";

  export default {
    name: "home",
    components: {
      TabControl,
      NavBar,
      HomeSwiper,
      RecommendView,
      FeatureView,
      GoodsList,
      Scroll,
      BackTop
    },
    data(){
      return{
        banner:[],
        recommends: [],
        goods:{
          'pop':{page:0 ,list:[]},
          'new':{page:0 ,list:[]},
          'sell':{page:0 ,list:[]},
        },
        currentType: 'pop',
        positiony: 0,
        tbtop: 0,
        SaveY :0,
      }
    },
    computed:{
      showGoods(){
        return this.goods[this.currentType].list
      },
      ishow(){
        return this.positiony >1000
      },
      tbShow(){
        return this.positiony > this.tbtop
      }
    },
    created() {
      //请求数据
      this.getHomeData();
      this.getHomeGoods('pop');
      this.getHomeGoods('new');
      this.getHomeGoods('sell');

    },
    mounted(){
      //监听图片加载完成，然后refresh bs的scroll height
      this.$bus.$on('homeimgLoaded',() => {
        this.$refs.scroll && this.$refs.scroll.scrollRefresh()
      })
    },
    activated(){
      this.$refs.scroll.scrollTo(0,this.SaveY,0)
      this.$refs.scroll.scrollRefresh()
    },
    deactivated(){
      this.SaveY = -(this.positiony)
    },
    methods:{
      //事件监听方法

      //tabcontrol的三种数据传导
      tabclick(index){
        switch (index){
          case 0: this.currentType = 'pop'
            break;
          case 1: this.currentType = 'new'
            break;
          case 2: this.currentType = 'sell'
            break
        }
        this.$refs.tabcontrol2.currentIndex = index;
        this.$refs.tabcontrol1.currentIndex = index;
      },

      //返回最上层的实现
      backclick(){
        this.$refs.scroll.scrollTo(0,0)
      },

      //返回最上层键位的消失和出现
      apear(position){
        this.positiony = (-position.y);
      },

      //上拉加载更多数据
      loadmore(){
        this.getHomeGoods(this.currentType);
      },
      spLoaded(){
        this.tbtop = this.$refs.tabcontrol1.$el.offsetTop
      },

      //网络请求相关方法

      getHomeData(){
        getHomeData().then(res =>{
          this.banner = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        })
      },
      getHomeGoods(type){
        const page = this.goods[type].page + 1;
        getHomeGoods(type,page).then(res => {
          this.goods[type].list.push(...res.data.list);
          this.goods[type].page += 1;
          this.$refs.scroll.scrollFinsh()
        })
      }
    }
  }
</script>

<style scoped>
  #home{
    /*padding-top: 44px;*/
    height: 100vh;
    position: relative;
  }
  .nav-bar{
    background-color: var(--color-tint);
    color: white;

    /*position: fixed;*/
    /*left: 0;*/
    /*right: 0;*/
    /*top: 0;*/
    /*z-index: 10;*/
  }
  /*.stick{*/
    /*position: sticky;*/
    /*top: 43px;*/
  /*}*/
  .tab-control{
    /*position: fixed;*/
    position: relative;
    z-index: 11;
  }

  .content{
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
</style>
